import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dark_mode_getx_demo/theme/app_theme_modes.dart';
import 'package:flutter_dark_mode_getx_demo/theme/theme_datas.dart';
import 'package:get/get.dart';

class AppThemeManager {
  static const String _kLocalThemeModeKey = "sp_key_app_theme";

  //获取本地存储的主题类型()
  static AppThemeMode getLocalAppThemeMode() {
    int? value = SpUtil.getInt(_kLocalThemeModeKey);
    AppThemeMode mode = AppThemeMode.of(value);
    return mode;
  }

  //获取本地存储的主题类型(系统)
  static ThemeMode getLocalThemeMode() {
    AppThemeMode mode = getLocalAppThemeMode();
    return mode.themeMode;
  }

  /*切换模式*/
  static changeMode(AppThemeMode mode) {
    ThemeMode curMode = getLocalThemeMode();
    ThemeMode targetMode = mode.themeMode;
    if (targetMode == curMode) {
      return;
    }

    //Get更新主题
    ThemeData themeData;
    if (targetMode == ThemeMode.system) {
      themeData = Get.isDarkMode ? kAppDarkThemeData : kAppLightThemeData;
    } else {
      themeData = targetMode == ThemeMode.dark ? kAppDarkThemeData : kAppLightThemeData;
    }
    Get.changeThemeMode(targetMode);
    Get.changeTheme(themeData);
    Future.delayed(const Duration(milliseconds: 200),(){
      Get.forceAppUpdate();
    });
    //更新本地存储
    SpUtil.putInt(_kLocalThemeModeKey, mode.value);
  }
}